/**
 * Created by Administrator on 2017/6/27.
 */
define(['jquery', 'vue','mui'], function ($, Vue,mui) {
    var order = "id";
    var sort = "desc";
    var data = {
        'order': order,
        "sort": sort,
        'page': 1,
        "flag_status":0
    };

    mui.init({
        pullRefresh: {
            container: '#refreshContainer',
            down: {
                height: 1,//可选,默认50.触发下拉刷新拖动距离,
                auto: false,//可选,默认false.自动下拉刷新一次
                contentdown: "正在刷新...",//可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
                contentover: "正在刷新...",//可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
                contentrefresh: "正在刷新...",//可选，正在刷新状态时，下拉刷新控件上显示的标题内容
                callback: pulldownRefresh//必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
            },
            up: {
                contentrefresh: '正在加载...',
                contentnomore: '没有更多数据了',//可选，请求完毕若没有更多数据时显示的提醒内容；
                callback: pullupRefresh
            }
        }
    });
    function refreshContainer() {
        mui('#refreshContainer').pullRefresh().refresh(true);
        mui('#refreshContainer').pullRefresh().scrollTo(0,0);
        reloadData();
    }
    var tab_vue = new Vue({
        el: '#tab-head',
        data: {
            t_index: 1,
        },
        methods: {
            tab_1: function (t_i) {
                if (vue.t_index !== t_i) {
                    data = {
                        'order': order,
                        "sort": sort,
                        'page': 1,
                        "flag_status": 0
                    };
                    tab_vue.t_index = t_i;
                    refreshContainer();
                }
            },
            tab_2: function (t_i) {
                if (vue.t_index !== t_i) {
                    data = {
                        'order': order,
                        "sort": sort,
                        'page': 1,
                        "flag_status": 1
                    };
                    tab_vue.t_index = t_i;
                    refreshContainer();
                }
            },
            tab_3: function (t_i) {
                if (vue.t_index !== t_i) {
                    data = {
                        'order': order,
                        "sort": sort,
                        'page': 1,
                        "flag_status":7
                    };
                    tab_vue.t_index = t_i ;
                    refreshContainer();
                }
            }
        }
    });
    var vue = new Vue({
        el: '#order_div',
        data: {
            items: []
        },
        methods: {
            detail: function (id) {
                window.location.href = detail_url + "/order_id/" + id;
            },
            delivery: function (id) {//开始配送
                $.post(delivery_url, {order_id: id},
                    function (result) {
                        if (result.code === 1) {
                            mui.alert("订单开始配送！!", '系统提示', function () {
                                tab_vue.tab_2(2);
                            }, 'div');
                        }else {
                            mui.alert(result.msg, '系统提示', function () {
                                location.reload();
                            }, 'div');
                        }
                    });
            }
        }
    });
    getData("up");
    function getData(flag) {
        $.get(list_url, data,
            function (result) {
                if (result.code === 1) {
                    switch (flag) {
                        case "up":
                            if (result.data.length>0) {
                                vue.items.push(result.data);
                                data.page++;
                                mui('#refreshContainer').pullRefresh().endPullupToRefresh();
                                $("#tip-icon").hide();
                            }
                            else {
                                // mui.toast("没有更多数据了");
                                if(data.page===1){
                                    $("#tip-icon").show();
                                    mui('#refreshContainer').pullRefresh().endPullupToRefresh();
                                }else {
                                    mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                                }
                            }
                            break;
                        case "down":
                            mui('#refreshContainer').pullRefresh().endPulldownToRefresh();
                            break;
                    }
                }
            });
    }

    // <editor-fold defaultstate="collapsed" desc="刷新数据">
    function reloadData() {
        data.page = 1;
        vue.items = [];
        getData("up");
    }

    // </editor-fold>
    /**
     * 上拉加载具体业务实现
     */
    function pullupRefresh() {
        getData("up");
    }

    /**
     * 下拉刷新具体业务实现
     */
    function pulldownRefresh() {
        getData("down");
    }
});